from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS
from django.conf import settings


# 验证邮箱链接
def generate_verify_email_url(user_id):
    # 加密token
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)
    token = s.dumps({'userid': user_id}).decode()
    callback_url = 'http:www.meiduo.site:8000/emails/verification/?token='
    verify_email_url = callback_url + token
    return verify_email_url


def decode_email_token(token):
    # 解密token
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)
    try:
        data = s.loads(token)
    except Exception as e:
        print(e)
        return None
    user_id = data.get('userid')
    return user_id
